package cn.houserent.controller;

import cn.houserent.entity.User;
import cn.houserent.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Map;
@Controller("userAction")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LoginController extends ActionSupport {
    private String name;
    private String password;
    @Autowired
    private UserService userService;



    public String login() throws Exception {
        ActionContext context = ActionContext.getContext();
        Map<String,Object> request = (Map<String,Object>)context.get("request");
        if (this.getName() == null || this.getName().equals("")){
            request.put("message","用户名不能为空");
            return "login_input";
        }
        if (this.getPassword() == null || this.getPassword().equals("")){
            request.put("message","密码不能为空");
            return "login_input";
        }
        Map<String, Object> session = context.getSession();
        User user = userService.login(name, password);
        if (user!=null){
            session.put("user",user);
            return "login_success";
        }
        request.put("message","用户名或者密码有误!!!");
        return "login_input";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
